﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Rename
{
    public partial class Form5 : Form
    {
        private ListView listView1;
        // 存储按钮状态的字典
        private Dictionary<Tuple<int, int>, bool> buttonStates = new Dictionary<Tuple<int, int>, bool>();
        public Form5()
        {
            InitializeComponent();

            // 初始化 ListView
            listView1 = new ListView();
            listView1.Dock = DockStyle.Fill;
            listView1.View = View.Details;
            listView1.FullRowSelect = true;
            listView1.GridLines = true;
            listView1.OwnerDraw = true; // 启用自定义绘制
            listView1.HeaderStyle = ColumnHeaderStyle.Nonclickable;

            // 添加列（最后一列用于显示按钮）
            listView1.Columns.Add("ID", 80);
            listView1.Columns.Add("名称", 200);
            listView1.Columns.Add("状态", 150);
            listView1.Columns.Add("操作", 120); // 按钮所在列

            // 添加示例数据
            for (int i = 0; i < 6; i++)
            {
                listView1.Items.Add(new ListViewItem(new[]
                {
                i.ToString(),
                $"项目 {i + 1}",
                i % 2 == 0 ? "启用" : "禁用",
                "" // 按钮列留空
            }));
            }

            // 绑定事件
            listView1.DrawColumnHeader += ListView1_DrawColumnHeader;
            listView1.DrawSubItem += ListView1_DrawSubItem;
            listView1.MouseDown += ListView1_MouseDown;
            listView1.MouseUp += ListView1_MouseUp;
            listView1.MouseLeave += ListView1_MouseLeave;

            this.Controls.Add(listView1);

            this.Text = "带按钮的 ListView";
            this.Size = new Size(800, 500);
        }
        // 绘制列头
        private void ListView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
        {
            e.Graphics.FillRectangle(Brushes.LightGray, e.Bounds);
            e.Graphics.DrawString(e.Header.Text, Font, Brushes.Black, e.Bounds,
                new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
            e.Graphics.DrawRectangle(Pens.Gray, e.Bounds);
        }

        private void ListView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
        {
            // 绘制普通子项
            if (e.ColumnIndex < 3)
            {
                e.DrawBackground();
                e.Graphics.DrawString(e.SubItem.Text, Font, Brushes.Black, e.Bounds,
                    new StringFormat { LineAlignment = StringAlignment.Center });
                return;
            }

            // 绘制按钮（最后一列）
            if (e.ColumnIndex == 3)
            {
                // 按钮区域
                Rectangle buttonRect = new Rectangle(
                    e.Bounds.X + 10,
                    e.Bounds.Y + 3,
                    e.Bounds.Width - 20,
                    e.Bounds.Height - 6
                );

                // 按钮状态（默认未按下）
                var key = Tuple.Create(e.Item.Index, e.ColumnIndex);
                bool isPressed = buttonStates.ContainsKey(key) && buttonStates[key];

                // 绘制按钮背景
                using (Brush brush = new SolidBrush(isPressed ? Color.LightSkyBlue : Color.AliceBlue))
                {
                    e.Graphics.FillRectangle(brush, buttonRect);
                }

                // 绘制按钮边框
                Pen pen = isPressed ? Pens.DarkBlue : Pens.LightBlue;
                e.Graphics.DrawRectangle(pen, buttonRect);

                // 绘制按钮文字
                e.Graphics.DrawString("编辑", Font, Brushes.Black, buttonRect,
                    new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
            }
        }
        // 鼠标按下 - 处理按钮点击
        private void ListView1_MouseDown(object sender, MouseEventArgs e)
        {
            //ListViewHitTestInfo hitTest = listView1.HitTest(e.Location);
            //if (hitTest.Item != null && hitTest.SubItem != null && hitTest.SubItem.Index == 3)
            //{
            //    // 标记按钮为按下状态
            //    var key = Tuple.Create(hitTest.Item.Index, hitTest.SubItem.Index);
            //    buttonStates[key] = true;
            //    listView1.Invalidate(hitTest.SubItem.Bounds); // 重绘按钮
            //}
        }
        // 鼠标释放 - 处理按钮点击事件
        private void ListView1_MouseUp(object sender, MouseEventArgs e)
        {
            //ListViewHitTestInfo hitTest = listView1.HitTest(e.Location);
            //if (hitTest.Item != null && hitTest.SubItem != null && hitTest.SubItem.Index == 3)
            //{
            //    var key = Tuple.Create(hitTest.Item.Index, hitTest.SubItem.Index);

            //    // 如果按钮曾被按下，则触发点击事件
            //    if (buttonStates.ContainsKey(key) && buttonStates[key])
            //    {
            //        OnButtonClicked(hitTest.Item.Index);
            //    }

            //    // 重置按钮状态
            //    buttonStates[key] = false;
            //    listView1.Invalidate(hitTest.SubItem.Bounds);
            //}

        }
        // 鼠标离开控件 - 重置所有按钮状态
        private void ListView1_MouseLeave(object sender, EventArgs e)
        {
            if (buttonStates.Count > 0)
            {
                buttonStates.Clear();
                listView1.Invalidate();
            }
        }
        // 按钮点击事件处理
        private void OnButtonClicked(int itemIndex)
        {
            ListViewItem item = listView1.Items[itemIndex];
            MessageBox.Show($"点击了第 {itemIndex + 1} 行的编辑按钮\n项目名称: {item.SubItems[1].Text}", "按钮点击");
        }
    }
}
